How to do it?:

Submission: Submit the link on Github of the assignment to Canvas

knitr::opts_chunk$set(message = FALSE)

  1. Install two packages gganimate and gifski then restart Rstudio. Use the WHO’s dataset at this link. Make a top-10 bar race by months between countries on the number of deaths by Covid 19.
library(tidyverse)
df <- read_csv('https://covid19.who.int/WHO-COVID-19-global-data.csv')
library(tidyverse)
library(gganimate)
library(lubridate)
df$month <- month(df$Date_reported)
names(df)
## [1] "Date_reported"     "Country_code"      "Country"          
## [4] "WHO_region"        "New_cases"         "Cumulative_cases" 
## [7] "New_deaths"        "Cumulative_deaths" "month"
da <- df %>% group_by(month, Country) %>% summarise(mean = mean(Cumulative_cases, na.rm=TRUE))
db <- da %>% group_by(month) %>% mutate(rank=rank(-mean)) %>% ungroup()
dc <- db %>% filter(rank <= 10)

a2 <- dc %>% ggplot(aes(x=rank, y=mean, group=Country, fill=Country, label=Country))+
    geom_col()+
    geom_text(aes( y = mean, label = Country), hjust = 1.4)+
    coord_flip(clip = 'off', expand = FALSE) +scale_x_reverse()+
    labs(title = 'month {closest_state}', x='', y='Cumulative_cases', fill='Country')+
    theme(plot.title = element_text(hjust = 1, size = 22),
          axis.ticks.y = element_blank(),
          axis.text.y = element_blank())+
transition_states(month)+
ease_aes("cubic-in-out")

animate(a2, nframes = 400)

  1. Make another bar race using that dataset.
library(gapminder)
library(gganimate)
library(ggplot2)
library(tidyverse)
library(lubridate)
library(knitr)
library(tidyverse)
df <- read_csv('https://covid19.who.int/WHO-COVID-19-global-data.csv')
df <- df %>% filter(New_deaths>0, New_cases>0)
library(lubridate)
df$Week <- week(df$Date_reported)
d1 <- df %>% group_by(Week, Country) %>% summarise(mean = mean(Cumulative_cases, na.rm=TRUE))
d2 <- d1 %>% group_by(Week) %>% mutate(rank=rank(-mean)) %>% ungroup()
d3 <- d2 %>% filter(rank <= 1000)
a1 <- d3 %>% ggplot(aes(x=rank, y=mean, group=Country, fill=Country, label=Country))+
     geom_col()+
     geom_text(aes(y = mean, label = Country), hjust = 1.4)+
     coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
     labs(title = 'Week {closest_state}', x='', y='Cumulative_cases', fill='Country')+
     theme(plot.title = element_text(hjust = 1, size = 22),
           axis.ticks.y = element_blank(),
           axis.text.y  = element_blank(), legend.position = "none") + 
     transition_states(Week)+
     ease_aes("cubic-in-out")
animate(a1, nframes=400)

  1. Make a bar race using a dataset of your own interest. You may use the dataset that we use in class (https://covidtracking.com/data/download/all-states-history.csv) but you should make a different bar race from ones in the slides.
library(tidyverse)
df3 <- read.csv('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv')
df3 <- df3 %>% filter(cases>0, deaths>0)
library(lubridate)
d11 <- df3 %>% group_by(date, state) %>% summarise(mean = mean(cases, na.rm=TRUE))
d12 <- d11 %>% group_by(state) %>% mutate(rank=rank(-mean)) %>% ungroup()
d13 <- d12 %>% filter(rank<=10)
a11 <- d13 %>% ggplot(aes(x=rank, y=mean, group=state, fill=state, label=state))+
  geom_col()+
  geom_text(aes(y = mean, label = state), hjust = 1.4)+
  coord_flip(clip = 'off', expand = FALSE) +scale_x_reverse()+
  labs(title = 'cases {closest_state}', x='', y='state', fill='state')+
  theme(plot.title = element_text(hjust = 1, size = 22),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(), legend.position = "none")+
  transition_states(date)+
  ease_aes("cubic-in-out")
animate(a11, nframes = 400)